package com.shunhao.utils

import android.app.Activity
import android.net.Uri
import android.text.TextUtils
import android.util.Base64
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ToastUtils
import com.shunhao.bean.ITakePhotoCallBack
import com.shunhao.bean.TakePhotoReturnBean
import com.sl.utakephoto.compress.CompressConfig
import com.sl.utakephoto.crop.CropOptions
import com.sl.utakephoto.exception.TakeException
import com.sl.utakephoto.manager.ITakePhotoResult
import com.sl.utakephoto.manager.UTakePhoto
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream

/**
 * ClassName:CameraUtils
 * Package:com.shunbega.utils
 * Description: 拍照 裁剪
 * Com:shunHao.com
 * @date:2020/10/9 15:54
 * @author:827746955@qq.com
 */
object  CameraUtils {

    /**
     * 拍照 FragmentActivity
     * @param activity
     */
    fun selectByCameraHeadImg(activity: FragmentActivity, iTakePhotoResult: ITakePhotoCallBack?): TakePhotoReturnBean {
        val mCropOptions = CropOptions.Builder().create()
            .apply {
                aspectX = 1
                aspectY = 1
                outputX = 100
                outputY = 100
                isUseOwnCrop = true
            }
        val takePhotoReturnBean = TakePhotoReturnBean()
        UTakePhoto.with(activity)
            .openCamera()
            .setCrop(mCropOptions)
            .setCompressConfig(CompressConfig.Builder().create())
            .build(object : ITakePhotoResult {

                override fun takeSuccess(uriList: List<Uri>) {
                    if (uriList.isNotEmpty()) {
                        val url = uriList[0]
                        val realPath = GetImagePath.getPath(context = activity,url)
                        LogUtil.d(tag = "CameraUtils","uri=${url.toString()} , path = $realPath")
                        takePhotoReturnBean.selectedUri = Uri.parse(realPath)
                        iTakePhotoResult?.onTakeSuccess(takePhotoReturnBean)
                    }
                }

                override fun takeFailure(ex: TakeException) {
                }
                override fun takeCancel() {}
            })
        return takePhotoReturnBean
    }

    /**
     * 拍照 FragmentActivity
     *
     * @param activity
     */
    fun selectByCamera(activity: FragmentActivity, iTakePhotoResult: ITakePhotoCallBack?): TakePhotoReturnBean {
        val takePhotoReturnBean = TakePhotoReturnBean()
        UTakePhoto.with(activity)
            .openCamera()
            .setCrop(CropOptions.Builder().create())
            .setCompressConfig(CompressConfig.Builder().create())
            .build(object : ITakePhotoResult {
                override fun takeSuccess(uriList: List<Uri>) {
                    if (uriList.isNotEmpty()) {
                        val url = uriList[0]
                        val realPath = GetImagePath.getPath(context = activity,url)
                        LogUtil.d(tag = "CameraUtils","uri=${url.toString()} , path = $realPath")
                        takePhotoReturnBean.selectedUri = Uri.parse(realPath)
                        iTakePhotoResult?.onTakeSuccess(takePhotoReturnBean)
                    }
                }

                override fun takeFailure(ex: TakeException) {}
                override fun takeCancel() {}
            })
        return takePhotoReturnBean
    }


    /**
     * 打开相册 FragmentActivity
     *
     * @param activity
     * @return
     */
    fun selectByAlbum(
        activity: FragmentActivity, iTakePhotoResult: ITakePhotoCallBack?): TakePhotoReturnBean {
        val takePhotoReturnBean = TakePhotoReturnBean()
        UTakePhoto.with(activity)
            .openAlbum()
            .setCrop(CropOptions.Builder().create())
            .setCompressConfig(CompressConfig.Builder().create())
            .build(object : ITakePhotoResult {
                override fun takeSuccess(uriList: List<Uri>) {
                    if (uriList.isNotEmpty()) {
                        val url = uriList[0]
                        val realPath = GetImagePath.getPath(context = activity,url)
                        LogUtil.d(tag = "CameraUtils","uri=${url.toString()} , path = $realPath")
                        takePhotoReturnBean.selectedUri = Uri.parse(realPath)
//                        takePhotoReturnBean.selectedBase64 =
//                            imageToBase64(url.path)
                        iTakePhotoResult?.onTakeSuccess(takePhotoReturnBean)
                    }
                }

                override fun takeFailure(ex: TakeException) {}
                override fun takeCancel() {}
            })
        return takePhotoReturnBean
    }

    /**
     * 打开相册 Activity
     *
     * @param activity
     * @return
     */
    fun selectByAlbumHeadImg(activity: Activity, iTakePhotoResult: ITakePhotoCallBack?): TakePhotoReturnBean {
        val mCropOptions = CropOptions.Builder().create()
            .apply {
                aspectX = 1
                aspectY = 1
                outputX = 100
                outputY = 100
                isUseOwnCrop = true
            }

        val takePhotoReturnBean = TakePhotoReturnBean()
        UTakePhoto.with(activity)
            .openAlbum()
            .setCrop(mCropOptions)
            .setCompressConfig(CompressConfig.Builder().create())
            .build(object : ITakePhotoResult {
                override fun takeSuccess(uriList: List<Uri>) {
                    if (uriList.isNotEmpty()) {
                        val url = uriList[0]
                        val realPath = GetImagePath.getPath(context = activity,url)
                        LogUtil.d(tag = "CameraUtils","uri=${url.toString()} , path = $realPath")
                        takePhotoReturnBean.selectedUri = Uri.parse(realPath)
                        //takePhotoReturnBean.selectedBase64 = imageToBase64(url.path)
                        iTakePhotoResult?.onTakeSuccess(takePhotoReturnBean)
                    }
                }

                override fun takeFailure(ex: TakeException) {}
                override fun takeCancel() {}
            })
        return takePhotoReturnBean
    }


    /**
     * 将图片转换成Base64编码的字符串
     */
    /*fun imageToBase64(path: String?): String? {
        if (TextUtils.isEmpty(path)) {
            return null
        }
        var `is`: InputStream? = null
        var data: ByteArray? = null
        var result: String? = null
        try {
            `is` = FileInputStream(path)
            //创建一个字符流大小的数组。
            data = ByteArray(`is`.available())
            //写入数组
            `is`.read(data)
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.DEFAULT)
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (null != `is`) {
                try {
                    `is`.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return result
    }*/
}